<script>
import { fetchRoleListAPI, addOrUpdateEmployeeAPI } from '@/api/system/employeeApi';

export default {
  name: 'employeeForm',
  props: {
    edit: {
      type: Boolean,
      required: true,
    },
  },
  data() {
    return {
      formData: {
        name: '',
        userName: '',
        roleId: '',
        password: '123456',
        status: 1,
        phonenumber: '',
      },
      formRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' },
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' },
        ],
        roleId: [
          { required: true, message: '请选择所属角色', trigger: 'change' },
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'change' },
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
        ],
      },
      roleList: [],
    };
  },
  mounted() {
    fetchRoleListAPI().then(data => {
      this.roleList = data.data;
    }).catch(error => {
      console.log(error.message);
    });
  },
  methods: {
    resetForm() {
      this.formData = {
        name: '',
        userName: '',
        roleId: '',
        roleName: '',
        password: '123456',
        status: 0,
        phonenumber: '',
      };
    },
    async handleSubmit() {
      const valid = await this.$refs.formRef.validate().catch(error => error);
      if (!valid) return;
      delete this.formData.password;
      delete this.formData.createTime;
      try {
        const { msg } = await addOrUpdateEmployeeAPI(this.formData);
        this.$message.success(msg);
        this.$emit('addSuccess');
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    findRole(row) {
      this.formData = { ...row };
    },
  },
  emits: ['addSuccess'],
};
</script>

<template>
  <el-form label-width="100px" :model="formData" :rules="formRules" ref="formRef">
    <el-form-item label="员工姓名" prop="name">
      <el-input placeholder="请输入员工姓名" size="small" v-model="formData.name"></el-input>
    </el-form-item>
    <el-form-item label="登录账号" prop="userName">
      <el-input placeholder="请输入登录账号" size="small" v-model="formData.userName"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="phonenumber">
      <el-input placeholder="请输入联系方式" size="small" v-model="formData.phonenumber"></el-input>
    </el-form-item>
    <el-form-item label="员工密码">
      <el-input disabled size="small" value="123456"></el-input>
    </el-form-item>
    <el-form-item label="分配角色" prop="roleId">
      <el-select v-model="formData.roleId" placeholder="请选择所属角色">
        <el-option v-for="(item) in roleList" :value="item.roleId" :label="item.roleName" :key="item.roleId"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="员工状态" prop="status">
      <el-radio-group v-model="formData.status">
        <el-radio :label="0">禁用</el-radio>
        <el-radio :label="1">启用</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button size="small">取消</el-button>
      <el-button type="success" size="small" @click="handleSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped lang="scss">
.el-select {
  width: 100%;
}
</style>
